package com.lihui;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/04/02  16:35
 */
@WebServlet("/showRequest")
public class ShowRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       StringBuilder sb = new StringBuilder();
        //方法名
        String method = req.getMethod();
        sb.append(method);
        sb.append("<br>");
        //获取URL
        String requestURI = req.getRequestURI();
        sb.append(requestURI);
        sb.append("<br>");
        //获取协议
        String protocol = req.getProtocol();
        sb.append(protocol);
        sb.append("<br>");
        //获取QueryString
        String queryString = req.getQueryString();
        sb.append(queryString);
        sb.append("<br>");
        //获取contextPath
        String contextPath = req.getContextPath();
        sb.append(contextPath);
        sb.append("<br>");
        //获取Header
        //获取的一个枚举类型，通过遍历获取每个key
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            //获取每一个key
            String headerKey = headerNames.nextElement();
            //通过key来获取value值
            String headerValue = req.getHeader(headerKey);
            //加入字符串
            sb.append(headerKey + ": " + headerValue);
            sb.append("<br>");
        }

        resp.setContentType("text/html");
        //写回响应
        resp.getWriter().write(sb.toString());
    }
}
